package com.lyshop.jinxiang.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lyshop.jinxiang.adapters.AddressListRVAdapter;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.base.BaseRVAdapter;
import com.lyshop.jinxiang.bean.AddressBean;
import com.lyshop.jinxiang.net.ActionCallbackListener;
import com.lyshop.jinxiang.utils.RouteUtil;
import com.lyshop.jinxiang.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class AddressListActivity extends BaseActivity implements BaseRVAdapter.OnItemClickListener, View.OnClickListener {
    private RecyclerView rv_list;
    private Button bt_add_address;
    private List<AddressBean> mList;
    private AddressListRVAdapter mAddressAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mList = new ArrayList<>();
        mAddressAdapter = new AddressListRVAdapter(mContext, mList);
        mAddressAdapter.setOnItemClickListener(this);
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        rv_list.setAdapter(mAddressAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        mAppAction.getAddressList(new ActionCallbackListener<List<AddressBean>>() {
            @Override
            public void onSuccess(List<AddressBean> data) {
                if (null != data) {
                    mList = data;
                    mAddressAdapter.setData(data);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showToast(mContext, message);
            }
        });
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_address_list;
    }

    @Override
    protected void findViews() {
        rv_list = findViewById(R.id.rv_list);
        bt_add_address = findViewById(R.id.bt_add_address);
    }

    @Override
    protected void setListeners() {
        bt_add_address.setOnClickListener(this);
    }

    @Override
    public void onItemClick(List data, View view, int position, long id) {
        Intent intent = new Intent();
        intent.putExtra(RouteUtil.BUNDLE_ADDRESS, mList.get(position));
        setResult(RESULT_OK, intent);
        backActivity();
    }

    @Override
    public void onClick(View v) {
        if (v == bt_add_address){
            RouteUtil.toAddAddressActivity(this, null);
        }
    }
}